<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */
?>
<div class="g_a_widget_header">
	<b><?php echo $this->TRANSLATOR->widget('current_path'); ?></b>
	<ul class="g_a_nav">
		<li class="g_a_first"><a href="javascript: void(0);" rel="" class="dirLink"><?php echo $this->TRANSLATOR->widget('root_dir', 'upload'); ?></a></li>
		<?php if (count($this->breakcrumb) > 0) : ?>
		<?php foreach ($this->breakcrumb as $index => $dir) : ?>
		<li><a href="javascript: void(0);" class="dirLink" rel="<?php echo Modules_Upload_Services_Directory::getPathLink(($index == 0) ? $temp = $dir : $temp .= DS . $dir); ?>"><?php echo $dir; ?></a></li>
		<?php endforeach; ?>
		<?php endif; ?>
	</ul>
	<div class="clear"></div>
	<ul class="g_a_choice_option">
		<li><a href="javascript: void(0);" class="grid<?php if ($this->style == 'grid') : ?>_active<?php endif; ?> styleLink" rel="grid"></a></li>
		<li><a href="javascript: void(0);" class="list<?php if ($this->style == 'list') : ?>_active<?php endif; ?> styleLink" rel="list"></a></li>
	</ul>	
	<div class="clearfix"></div>
	<p><?php echo $this->TRANSLATOR->widget('guide'); ?></p>	
	<div class="clear"></div>				
</div>
<div class="g_a_widget_content">
	<div class="g_a_widget_content_medium">
		<?php if ($this->data) : ?>
		<ul>
		<?php foreach ($this->data as $index => $item) : ?>
		<?php if ($index >= $this->start && $index < $this->end) : ?>
			<?php if ($index % 3 == 0 && $index > 0) : ?>
			</ul><ul>
			<?php endif; ?>
			<li>
				<?php if ($item['is_dir']) : ?>
				<a title="<?php echo $this->STRING->escape($item['name']); ?>" href="javascript: void(0);" class="dirLink" rel="<?php echo Modules_Upload_Services_Directory::getPathLink($this->path . DS . $item['name']); ?>">
					<img src="<?php echo sprintf('%s/modules/%s/widgets/%s/css/images/folder_icon.jpg', $this->APP_STATIC_SERVER, $this->module, $this->widgetName); ?>" />
					<br /><?php echo $this->STRING->subString($item['name'], 10); ?>
				</a>
				<?php else : ?>
				<a class="g_upload_browsetoolbox_draggable nyroModal" rel="<?php echo sprintf('%s/%s/%s', $this->APP_RESOURCE_SERVER, str_replace(DS, '/', $this->path), $item['name']); ?>" href="<?php echo sprintf('%s/%s/%s', $this->APP_RESOURCE_SERVER, str_replace(DS, '/', $this->path), $item['name']); ?>" title="<?php echo $this->STRING->escape($item['name']); ?>">
					<img alt="<?php echo $this->STRING->escape($item['name']); ?>" onerror="this.src='<?php echo sprintf('%s/images/icons/unknown.png', $this->APP_STATIC_SERVER); ?>';" src="<?php echo sprintf('%s/images/icons/%s.png', $this->APP_STATIC_SERVER, strtolower(substr($item['name'], -3))); ?>" />
				</a>
				<?php endif; ?>
			</li>
		<?php endif; ?>								
		<?php endforeach; ?>
		</ul>
		<?php endif; ?>
		
		<div class="g_a_bottom t_a_ui_paginator">
			<?php //echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
		</div>
	</div>
</div>
<div class="clear"></div>
<div><?php echo $this->fullPath; ?></div>
<script type="text/javascript">
'Gio.Upload.Widgets.FileToolbox.load'.namespace();

Gio.Upload.Widgets.FileToolbox.load.handleDrop = function(droppedElement, event, ui) {
	if (!$(ui.helper).hasClass('g_upload_browsetoolbox_draggable')) {
		return;
	}
	
	var data = $(ui.helper).attr('rel');
		
	switch (true) {
		/** 
		 * Textbox, textarea
		 */
		case ($(droppedElement).attr('type') == 'text' || $(droppedElement).attr('type') == 'textarea'):
			$(droppedElement).val($(droppedElement).val() + data);
			break;
			
		/** 
		 * WYSIWYG
		 */
		case ($(droppedElement).find('textarea.g_a_ui_editor').length > 0):
			var id      = $(droppedElement).find('textarea.g_a_ui_editor:first').attr('id');
			var ext 	= data.substring(data.length - 3, data.length);
			switch (ext.toLowerCase()) {
				case 'jpg':
				case 'png':
				case 'bmp':
				case 'gif':
					data = '<img src="' + data + '" />';
					break;
				case 'mp4':
				case 'flv':
					break;
				case 'mp3':
					break;
				default:
					data = '<a href="' + data + '" title="' + data + '">' + data + '</a>';
					break;
			}
			//var insertHtml = 
			var factory = new Gio.Core.Editor.Factory();
			factory.factory('<?php echo $this->editor; ?>')
				   .setId(id)
				   .insertHtml(data);
			break;
	}
};

$(document).ready(function() {
	$('.dirLink').bind('click', function() {
		var path = $(this).attr('rel');
		Gio.Upload.Widgets.FileToolbox.loadWidgets(path, 1, '<?php echo $this->style?>');
	});
	
	$('.styleLink').bind('click', function() {
		var style = $(this).attr('rel');
		Gio.Upload.Widgets.FileToolbox.loadWidgets('<?php echo Modules_Upload_Services_Directory::getPathLink($this->path); ?>', 1, style);
	});

	/**
	 * Make the textbox, textarea, WYSIWYG editors droppable
	 */
	$([
		$('input[type="text"], textarea'),
		$('.g_a_ui_editor').parent()
	]).each(function() {
		$(this).droppable({
			drop: function(event, ui) {
				Gio.Core.ToolBox.callDropHandler($(this), 'Gio.Upload.Widgets.FileToolbox.load.handleDrop', event, ui);
			}
		})
	});

	$('a.nyroModal').nyroModal();
});
</script>